local snow    = require("meet_your_mods.mhr").snow
local status  = require("meet_your_mods.status")
local setting = require("meet_your_mods.setting")
local ui      = require("meet_your_mods.ui")

local spider_man = {
    setting = {
        unlimited_wirebug = true, --闲暇时翔虫无限
        unlimited_travel  = true, --侦察队传送次数无限
    },

    fast_traveled = false,
}

function spider_man:reset_cooldown(_)
    for _, wirebug in pairs(snow.player.PlayerManager:get_singleton():findMasterPlayer()._HunterWireGauge) do
        wirebug._RecastTimer = 0
    end
end

function spider_man:post_start(result)
    if self.setting.unlimited_wirebug and status:is_leisure() then
        self:reset_cooldown()
    end

    return result
end

function spider_man:pre_setQuestClear(_)
    if self.setting.unlimited_wirebug then
        self:reset_cooldown()
    end
    return sdk.PreHookResult.CALL_ORIGINAL
end

function spider_man:pre_onCompleteReconOtomoAct(parameters)
    if self.setting.unlimited_travel
        and snow.data.OtomoReconManager:get_singleton():get_IsOtomoReconQuest()
    then
        self.fast_traveled = sdk.to_managed_object(parameters[2]):get_IsFastTraveled()
    end
    return sdk.PreHookResult.CALL_ORIGINAL
end

function spider_man:post_onCompleteReconOtomoAct(result)
    if self.setting.unlimited_travel and self.fast_traveled then
        snow.data.OtomoReconManager:get_singleton()._IsUseOtomoReconFastTravel = false
    end

    return result
end

function spider_man:pre_onQuestEnd(_)
    if self.setting.unlimited_travel
        and snow.data.OtomoReconManager:get_singleton():get_IsOtomoReconQuest()
        and not snow.otomo.OtomoManager:get_singleton():get_RefOtReconManager():get_ReconOtomo()
    then
        snow.data.OtomoReconManager:get_singleton():setUseOtomoReconFastTravel()
    end

    return sdk.PreHookResult.CALL_ORIGINAL
end

function spider_man:initialize()
    snow.player.fsm.PlayerFsm2ActionHunterWire.start:sdk_auto_hook(self)
    snow.QuestManager.setQuestClear:sdk_auto_hook(self)
    snow.QuestManager.onQuestEnd:sdk_auto_hook(self)
    snow.otomo.OtomoReconCharaManager.onCompleteReconOtomoAct:sdk_auto_hook(self)

    setting:bind_setting("spider_man", self.setting)

    ui:cluster("easy_quest", "任务便利功能")
    ui:checkbox("easy_quest", "spider_man", "unlimited_wirebug", "闲暇时翔虫无限")
    ui:checkbox("easy_quest", "spider_man", "unlimited_travel",  "侦察队传送次数无限")
end

function spider_man:normalize()
end

return spider_man